/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "1111",
        deptName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects','element'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var formSelects = layui.formSelects;
    var element = layui.element;




    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {

        var ajax = new $ax(Feng.ctxPath + "/share/copySchedule" ,function (data) {

            //400错误合成失败
            if (data.code ==400){
                alert(data.message+'!!!'+data.data);
            } else {
                Feng.success("合成成功！");
                admin.closeThisDialog();
            }


        }, function (data) {
            Feng.error("合成失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
        //刷新父页面
        // parent.location.reload();
        // 添加 return false 可成功跳转页面
        // layer.close(loadIndex);
        return false;
    });



    /**
     * 初始化日程框架
     */
    var otherOrMyself='';

    /**
     * 初始化日程
     * @param data
     */
    loadSchedule();
    function loadSchedule(){
        $.ajax({
            url:Feng.ctxPath + "/share/getOtherSchedule",
            success:function (message) {
                $('#scheduleName').append('<option value="">请选择日程</option>');
                $.each(message.data, function(index, item) {

                    $('#scheduleName').append('<option value="'+item.scheduleId+'">'+item.scheduleName+'</option>');
                });
                //重新渲染表单
                form.render('select');
                if (Feng.getUrlParam("scheduleId")!=null){
                    form.val("userForm",{"scheduleId":Feng.getUrlParam("scheduleId")});
                    showFrame(Feng.getUrlParam("scheduleId"));
                }
            }
        })
    }

    form.on('select(getFrame)', function(data) {
        $('#scheduleMessage').empty();
        if (data.value!='') {
            showFrame(data.value)
        }
    });


    /**
     * 显示一周样式
     */
    function showFrame(data){
        $.ajax({
            url: Feng.ctxPath + "/share/share_getScheduleAndFrameitem",
            data: {"scheduleId": data},
            success: function (message) {
                form.val("userForm",{"startData":message.data.startData,"endData":message.data.endData});

                schedulemessage = '';
                schedulemessage += '<div class="layui-inline layui-col-md6">';
                schedulemessage += '    <label class="layui-form-label">开始时间</label>';
                schedulemessage += '    <div class="layui-input-block">';
                schedulemessage += '        <input  type="text" class="layui-input date-icon datetime" autocomplete="off" disabled value="'+message.data.startData+'" />';
                schedulemessage += '    </div>';
                schedulemessage += '</div>';
                schedulemessage += '<div class="layui-inline layui-col-md6">';
                schedulemessage += '    <label class="layui-form-label">结束时间</label>';
                schedulemessage += '    <div class="layui-input-block">';
                schedulemessage += '        <input  type="text" class="layui-input date-icon datetime" autocomplete="off" disabled value="'+message.data.endData+'" />';
                schedulemessage += '    </div>';
                schedulemessage += '</div>';
                schedulemessage += '<div class="layui-inline layui-col-md12">';
                schedulemessage += '    <label class="layui-form-label">描述</label>';
                schedulemessage += '    <div class="layui-input-block">';
                schedulemessage += '        <input placeholder="请输入内容" class="layui-input" value="'+message.data.description+'" disabled/>';
                schedulemessage += '    </div>';
                schedulemessage += '</div>';

                schedulemessage += '<table class="layui-table" >';
                schedulemessage += '   <colgroup><col width="150"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"></colgroup>';
                schedulemessage += '   <thead><tr ><th></th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th><th>星期日</th></tr></thead>';

                $.each(message.data.frameitems, function (index, item) {
                    schedulemessage += '<tr><td >时间：' + item.startTime + '-' + item.endTime + '</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
                });
                $("#scheduleMessage").append(schedulemessage);
                form.render();
            }
        })
    }


    timeAdd();
    //初始化时间框
    function timeAdd(){
        lay('.datetime').each(function() {
            //时间选择器
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'date'
            });
        });
    }

    //初始化我的日程
    formSelects.config('selMySchedule', {
        searchUrl: Feng.ctxPath + "/schedule/schedule_list",
        keyName: 'scheduleName',
        keyVal: 'scheduleId'
    });
    //初始化其他的日程
    // formSelects.config('selOtherSchedule', {
    //     searchUrl: Feng.ctxPath + "/share/getShareSchedule?otherOrmyself=other",
    //     keyName: 'scheduleName',
    //     keyVal: 'scheduleId'
    // });

});